package io.zcoder.innoway.activities;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.zcoder.innoway.R;
import io.zcoder.innoway.commons.PrefUtils;
import io.zcoder.innoway.models.ResponseBase;
import io.zcoder.innoway.rest.RestClient;
import io.zcoder.innoway.utils.Utils;
import io.zcoder.innoway.commons.Constants;
import io.zcoder.innoway.widgets.ScrollSelectWeightView;
import io.zcoder.innoway.widgets.TextViewCustom;
import retrofit.Callback;
import retrofit.RetrofitError;
import retrofit.client.Response;

public class SetGoalActivity extends BaseActivity implements ScrollSelectWeightView.SelectCallBack {
    @BindView(R.id.keep_text)
    TextViewCustom mKeepText;
    @BindView(R.id.lose_text)
    TextViewCustom mLoseText;
    @BindView(R.id.gain_text)
    TextViewCustom mGainText;
    @BindView(R.id.view_line_keep)
    View mKeepLine;
    @BindView(R.id.view_line_lose)
    View mLoseLine;
    @BindView(R.id.view_line_gain)
    View mGainLine;
    @BindView(R.id.current_weight)
    TextViewCustom mCurrentWeight;
    @BindView(R.id.current_unit)
    TextViewCustom mCurrentUnit;
    @BindView(R.id.weight_ideal)
    TextViewCustom mIdeal;
    @BindView(R.id.select_weight)
    ScrollSelectWeightView mSelectWeight;

    @OnClick(R.id.set_goal)
    void setGoal() {
        showProgress();
        RestClient.build(this, true).setGoal(PrefUtils.getInt(this, PrefUtils.PREF_PROFILE_ID),
                Utils.formatFloat(mWeightSelected / Utils.getRatioWeight(mUnit)), new Callback<ResponseBase>() {
                    @Override
                    public void success(ResponseBase responseBase, Response response) {
                        hideProgress();
                        if (responseBase.getStatus_code() != 200) {
                            showAlert(getString(R.string.error), responseBase.getMessage());
                        } else {
                            setResult(RESULT_OK);
                            finish();
                        }
                    }

                    @Override
                    public void failure(RetrofitError error) {
                        hideProgress();
                    }
                });

    }

    private int mUnit;
    private float mWeight;
    private float mWeightSelected;
    private float mGoal;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_set_goal_activity);
        ButterKnife.bind(this);
        setTitle(getString(R.string.set_goal));
        showBack(true);

        mWeight = getIntent().getFloatExtra(Constants.WEIGHT_EXTRAS, 0);
        mCurrentWeight.setText(mWeight + "");
        mUnit = getIntent().getIntExtra(Constants.UNIT_EXTRAS, 0);
        mCurrentUnit.setText(Utils.getUnitWeight(mUnit));
        String ideal = getIntent().getStringExtra(Constants.IDEAL_EXTRAS);
        mIdeal.setText(ideal == null ? "" : ideal);
        mGoal = getIntent().getFloatExtra(Constants.GOAL_EXTRAS, 0);

        mSelectWeight.initData(mGoal, mUnit, this);
        onSelected();
    }

    @Override
    public void onSelected() {
        mWeightSelected = Float.valueOf(mSelectWeight.getValue());
        mLoseLine.setVisibility(View.GONE);
        mGainLine.setVisibility(View.GONE);
        mKeepLine.setVisibility(View.GONE);
        mKeepText.setTextColor(getResources().getColor(R.color.gray63));
        mLoseText.setTextColor(getResources().getColor(R.color.gray63));
        mGainText.setTextColor(getResources().getColor(R.color.gray63));
        if (mWeight == mWeightSelected) {
            mKeepText.setTextColor(getResources().getColor(R.color.bg_select_tall_btn));
            mKeepLine.setVisibility(View.VISIBLE);
        } else if (mWeight > mWeightSelected) {
            mLoseText.setTextColor(getResources().getColor(R.color.bg_select_tall_btn));
            mLoseLine.setVisibility(View.VISIBLE);
        } else if (mWeight < mWeightSelected) {
            mGainText.setTextColor(getResources().getColor(R.color.bg_select_tall_btn));
            mGainLine.setVisibility(View.VISIBLE);
        }
    }
}
